
var phoneFormats = [
	['<country> ', '(<area>) <prefix>-<suffix>', ' x <ext>'],
	['<country>-', '<area>-<prefix>-<suffix>', ' x <ext>'],
	['<country>.', '<area>.<prefix>.<suffix>', ' x <ext>']
];

function FormatPhoneNumber( format, number ) {
	var	country		= number[0],
		area		= number[1],
		prefix		= number[2],
		suffix		= number[3],
		ext			= number[4];
	
	var result = '';
	
	if( country ) {
		result += format[0].replace( '<country>', country );
	}
	
	result += format[1]
				.replace( '<area>', area )
				.replace( '<prefix>', prefix )
				.replace( '<suffix>', suffix );
	
	if( ext ) {
		result += format[2].replace( '<ext>', ext );
	}
	
	return result;
}

function StripPhoneNumber( number ) {
	var matchRegex = /^(1)?(\d{3})(\d{3})(\d{4})(\d+)?/;
	var stripRegex = /[^0-9]/g;
	
	number = number.replace( stripRegex, '' );
	var matches = number.match( matchRegex );
	if( matches ) {
		return matches.slice( 1, 6 );
	}
}

function GetDisplayNumber( format ) {
	var number = ['1', '222', '333', '4444', '555'];
	return FormatPhoneNumber( format, number );
}

function CreateSingleRadioButton( index, format ) {
	var uid = 'phone-format-radio';
	
	var input = $('<input>', {
		id: uid + '-' + index,
		type: 'radio',
		name: uid,
		value: index
	});
	
	var label = $( '<label>', {
		for: uid + '-' + index,
		text: GetDisplayNumber( format )
	});
	
	return input.after( label ).after( '<br/>' );
}

function CreatePhoneFormatRadioButtons( id ) {
	for( var i = 0, format; format = phoneFormats[i]; ++i ) {
		var input = CreateSingleRadioButton( i, format );
		$(id + ' > button:first').before( input );
	}
}